package sample1;
import lovedna.game.Actor;
import lovedna.game.Material;
import lovedna.game.Mesh;
import lovedna.game.Shader;
import lovedna.game.ShaderCode;
import lovedna.game.components.MeshRenderer;
import lovedna.game.geometry.GeomSphere;
import lovedna.game.graphics.GLState;
import lovedna.resource.Assets;

/**
 * ... 环境反射
 * @author liwei
 */
class Reflection extends Actor
{
	/*======constants============================*/
	
	/*======static ==============================*/
	
	/*======Public===============================*/
	
	/*======Private==============================*/
	
	public function new() 
	{
		super();
		Client.instance.camera.actor.addComponent(lovedna.game.components.CameraControl);
		
		var vert:String = '
		attribute vec3 position:POSITION;
attribute vec3 normal:NORMAL;
attribute vec2 uv:UV;
varying vec3 vuv;
varying vec3 vPosition;
varying vec3 vNormal;
uniform mat4 matrixM:M;
uniform mat4 matrixVP:VP;
uniform vec4 screen:TARGET_PARAMS;

void main(void){
	gl_Position = matrixVP * (matrixM * vec4(position, 1.0));
	//gl_Position = pixelSnap(gl_Position, screen);
	vPosition=(matrixM*vec4(position,1.0)).xyz;
	vNormal=normalize(matrixM*vec4(normal,0.0)).xyz;
}
		';
		var frag:String = '
uniform samplerCube _Texture0:TEX;
varying vec3 vuv;
varying vec3 vPosition;
varying vec3 vNormal;
uniform vec3 _CameraPos:EYE;
uniform vec4 refractValue:TIME_SIN;
void main(void){
	//vec3 ref=refract(normalize(vPosition-_CameraPos),vNormal,refractValue.z);
	gl_FragColor = textureCube(_Texture0, vNormal);
}
		';
		
		
		
		var actor:Actor = new Actor();
		var mr:MeshRenderer = actor.addComponent(MeshRenderer);
		mr.material.renderQueue = Material.Background;
		mr.material.shader = new Shader(new ShaderCode("", vert, frag));
		mr.material.state.cullMode = GLState.CULLMODE_FRONT;
		mr.material.setTexture(Assets.cube.get("image/cube/pisa.png").texture);
		mr.mesh = new Mesh();
		var s = new GeomSphere();
		s.radius = 300;
		mr.mesh.fromBuffer(s.build());
		actor.setParent(Client.instance.camera.actor);
		
		
		var frag:String = '
uniform samplerCube _Texture0:TEX;
varying vec3 vuv;
varying vec3 vPosition;
varying vec3 vNormal;
uniform vec3 _CameraPos:EYE;
uniform vec4 refractValue:TIME_SIN;
void main(void){
	vec3 ref=reflect(vPosition-_CameraPos,vNormal);
	gl_FragColor = textureCube(_Texture0, ref);
}
		';
		
		var actor:Actor = new Actor();
		var mr:MeshRenderer = actor.addComponent(MeshRenderer);
		mr.material.shader = new Shader(new ShaderCode("", vert, frag));
		//mr.material.state.cullMode = GLState.CULLMODE_FRONT;
		mr.material.setTexture(Assets.cube.get("image/cube/pisa.png").texture);
		mr.mesh = new Mesh();
		var s = new GeomSphere();
		s.radius = 30;
		mr.mesh.fromBuffer(s.build());
		//actor.transform.rotation.setEulerAngles(0, 45, 40);
		actor.setParent(this);
		
	}
	/*======Public Static methods================*/
	
	/*======Public methods=======================*/
	
	/*======public override methods==============*/
	
	/*======private override methods=============*/
	
	/*======Private methods======================*/
	
	/*======get set methods======================*/
	
}